<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>编辑入库信息</title>
    <script type="text/javascript" src="/js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="/js/bootstrap.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="/css/style.css"/>
    <style>
        label span {
            color: #ff0000;
            padding-right: 5px;
        }
    </style>
</head>

<body>
<h2>编辑入库信息</h2>
<form id="myForm" class="form-horizontal" method="post">
    <input type="hidden" name="in_num">
    <div class="form-group">
        <label class="col-sm-1 control-label"><span>*</span>入库单号</label>
        <div class="col-sm-9">
            <input required="required" name="in_id" class="form-control" placeholder=""/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label"><span>*</span>商品编号</label>
        <div class="col-sm-9">
            <input required="" name="p_id" class="form-control" placeholder=""/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">入库数</label>
        <div class="col-sm-9">
            <input name="in_number" class="form-control" placeholder=""/>
        </div>
    </div>

    <!--时间-->
    <div class="form-group">
        <label class="col-sm-1 control-label">入库时间</label>
        <div class="col-sm-5">
            <input name="in_time" class="form-control" type="datetime-local"/>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-1 control-label">备注</label>
        <div class="col-sm-9">
            <input name="in_remarks" class="form-control"/>
        </div>
    </div>

    <!--仓库选择-->
    <div class="form-group">
        <label class="col-sm-1 control-label">仓库</label>
        <div class="col-sm-2">
            <select name="w_id" class="form-control">
                <!--<option value="-1">请选择仓库</option>-->
                <!--<option value="0" name="">环东路</option>-->
                <!--<option value="1" name="">门市</option>-->
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-1 control-label"></label>
        <div class="col-sm-9">
            <button id="btnUpdate" type="button" class="btn btn-sm btn-info">提&nbsp;交</button>
        </div>
    </div>

</form>

</body>
</html>
<script>

    String.prototype.GetValue = function (para) {
        let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?") + 1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    $(function () {
        queryByW_name();
        let url = document.location.toString();
        let in_num = url.GetValue("in_num");
        setTimeout(function () {
            loadData(in_num);
        }, 1000);


        //提交按钮的单击事件
        $("#btnUpdate").click(function () {
            $.ajax({
                type: "POST",
                url: "/inpstock",
                data: $("#myForm").serialize()+"&_method=PUT",
                dataType: "json",
                success: function (vo) {
                    if (vo.code == 200) {
                        alert("更新成功！");
                        window.location.href = "/pages/inpstock/list.html?pageNum=" + pageNum + "&pageSize=" + pageSize;
                    } else {
                        alert("更新失败！" + vo.msg);
                    }
                }
            });
        });


    });

    function queryByW_name() {
        $.ajax({
            type: "GET",
            url: "/warehouse/",
            data: null,
            dataType: "json",
            success: function(vo){
                let list = vo.list;
                let str = "<option value=\"-1\">请选择仓库</option>";
                for(let i=0 ; i < list.length ; i++){
                    let obj = list[i];
                    let w_name = obj.w_name;
                    let w_id = obj.w_id;
                    str += '<option value="'+w_id+'" name="'+w_name+'">'+w_name+'</option>';
                }
                $("select[name='w_id']").html(str);
            }
        });
    }

    function loadData(in_num) {
        $.ajax({
            type: "GET",
            url: "/inpstock/" + in_num,
            data: "",
            success: function (vo) {
                let obj = vo.obj;
                $("input[name='in_num']").val(in_num);
                $("input[name='in_id']").val(obj.in_id);
                $("input[name='p_id']").val(obj.p_id);
                $("input[name='in_number']").val(obj.in_number);
                $("input[name='in_time']").val(obj.in_time);
                $("input[name='in_remarks']").val(obj.in_remarks);
                $("select[name='w_id']").val(obj.w_id);
            }
        });
    }
</script>